import java.util.Arrays;

public class Learn17 {
    public static int myBinarySearch(int[] array,int key){
        int left = 0;
        int right = array.length-1;
        while(left <= right){
            int mid = (left + right)/2;
            if(key < array[mid]){
                right = mid - 1;
            } else if (key > array[mid]) {
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
//        System.out.println(Arrays.binarySearch(array,5));
        int ret = myBinarySearch(array,5);         //二叉查找，必须有序
        System.out.println(ret);
    }
}
